package de.ohmhochschule.bme;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.*;
import android.widget.Button;
import android.widget.EditText;

public class WebBrowserActivity extends Activity {

	private EditText editText;
	private WebView webView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		editText = (EditText) findViewById(R.id.editText1);
		webView = (WebView) findViewById(R.id.webView1);
		webView.setWebViewClient( new HelloWebViewClient() );

		Button button = (Button) findViewById(R.id.btn_Go);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String sUri = editText.getText().toString();
				if (!sUri.startsWith("http://")) {
					sUri = "http://" + sUri;
				}
				webView.loadUrl(sUri);
				webView.requestFocus();
			}
		});
	}

	/**
	 * this makes sure that you are not opening an exteral browser when clicking on links.
	 * see android-sdk/docs/resources/tutorials/views/hello-webview.html
	 */
	private class HelloWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}
}